package com.lsq.wuyuge.controller;


import com.lsq.wuyuge.common.result.Result;
import com.lsq.wuyuge.context.BaseContext;
import com.lsq.wuyuge.dto.PasswordDTO;
import com.lsq.wuyuge.util.JwtUtil;
import com.lsq.wuyuge.dto.UserInfoDTO;
import com.lsq.wuyuge.dto.UserLoginDTO;
import com.lsq.wuyuge.dto.UserRegisterDTO;
import com.lsq.wuyuge.entity.User;
import com.lsq.wuyuge.propertis.JwtPropertis;
import com.lsq.wuyuge.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 一声闷雷
 * @since 2025-03-15
 */
@RestController
@RequestMapping("/user")
@Slf4j
@CrossOrigin(origins = "http://localhost:3000")
public class UserController {

    @Autowired
    private IUserService  userService;
    @Autowired
    private JwtPropertis jwtPropertis;

    /**
     * 用户注册
     * @return
     */
    @PostMapping("/register")
    public Result<UserInfoDTO>  register(@RequestBody UserRegisterDTO  userRegisterDTO){
        log.info("用户注册");
        User user = new User();
        BeanUtils.copyProperties(userRegisterDTO,user);
        userService.save(user);
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        BeanUtils.copyProperties(user,userInfoDTO);
        //jwt
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId",user.getId());
        String token=JwtUtil.createToken(
                jwtPropertis.getSecretKey(),
                jwtPropertis.getTtlMillis(),
                claims);
        userInfoDTO.setToken(token);
        return Result.success(userInfoDTO);
    }

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<UserInfoDTO>  login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户登录");
        UserInfoDTO userInfoDTO = userService.userLogin(userLoginDTO);
        return Result.success(userInfoDTO);
    }

    /**
     * 用户登出
     * @return
     */
    @PostMapping("/logout")
    public Result<String>  logout(){
        log.info("用户登出");
        return Result.success("退出成功");
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/info")
    public Result<UserInfoDTO>  info(){
        log.info("获取用户信息");
        Integer userId = BaseContext.getCurrentId().intValue();
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        BeanUtils.copyProperties(userService.getById(userId),userInfoDTO);
        return Result.success(userInfoDTO);
    }

    /**
     * 更新用户信息
     * @return
     */
    @PutMapping("/info")
    public Result<Boolean> update(@RequestBody User  user){
        log.info("更新用户信息");
        Boolean isUpdate = userService.updateById(user);userService.updateByUser(user);
        return Result.success(isUpdate);
    }

    /**
     * 修改用户密码
     * @return
     */
    @PutMapping("/password")
    public Result<String> updatePassword(@RequestBody PasswordDTO passwordDTO){
        log.info("修改用户密码");
        Integer userId = BaseContext.getCurrentId().intValue();
        userService.updatePassword(passwordDTO.getOldPassword(),passwordDTO.getNewPassword(),userId);
        return Result.success();
    }
    /**
     * 更新用户头像
     * @return
     */
    @PostMapping("/avatar/{avatar}")
    public Result<String> updateAvatar(@PathVariable String avatar) {
        log.info("更新用户信息");
        Integer userId = BaseContext.getCurrentId().intValue();
        userService.updateAvatar(avatar,userId);
        return Result.success();
    }

//    /**
//     * 获取用户ID
//     * @return
//     */
//    public Integer getUserId() {
//        log.info("获取用户ID");
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String token = request.getHeader(jwtPropertis.getTokenName());
//        log.info("token:{}",token);
//        Map<String, Object> claims = JwtUtil.paresToken(token, jwtPropertis.getSecretKey());
//        Integer userId = (Integer) claims.get("userId");
//        log.info("userId:{}",userId);
//        return userId;
//    }

}
